<?php
/**
 *客户管理
*/

namespace app\admin\controller;
use PhpOffice\PhpWord\TemplateProcessor;
use app\admin\controller\Admin;
use think\facade\App;
use think\facade\Db;
use xhadmin\service\admin\record\CustomerService;
use xhadmin\db\Customer as CustomerDb;
use think\facade\Cache;

class Customers extends Admin {


	/*会员管理*/
	function index(){
        //p(session('admin'));exit;

        if (!$this->request->isAjax()){
			return $this->display('customers/index');
		}else{
			$limit  = $this->request->post('limit', 0, 'intval');
			$offset = $this->request->post('offset', 0, 'intval');
			$page   = floor($offset / $limit) +1 ;

			$id_card= $this->request->param('id_card', '', 'strip_tags,trim');
			$first_name = $this->request->param('first_name', '', 'strip_tags,trim');
			$birth_year = $this->request->param('birth_year', '', 'strip_tags,trim');
            $id= $this->request->param('id', '', 'strip_tags,trim');

            //$last_name = $this->request->param('last_name', '', 'strip_tags,trim');
            if(!empty($id_card)){
                $where['id_card'] = ['like','%'.$id_card.'%'];
            }
            if(!empty($birth_year)){
                $where['birth_day'] = ['like','%'.$birth_year.'%'];
            }
            if(!empty($first_name)){
                $where['first_name|last_name|passport_info'] = ['like','%'.$first_name.'%'];
            }
            if(!empty($id)){
                $where['id'] = ['=',$id];
            }
            //$where['status'] = ['=',0];
			$limit = ($page-1) * $limit.','.$limit;
            //p($where);exit;
			try{
				$res = CustomerService::pageList(formatWhere($where),$limit,'*',$orderby = 'id desc');
				foreach ($res['list'] as $key=>&$val) {
				    $val['work_year_month'] = $val['work_year'].'年'.$val['work_month'].'个月'.$val['work_times'].'次';
                    $val['name'] = $val['first_name'].'</br> '.$val['last_name'];
                    $val['status'] = isset(self::$status[$val['status']])?self::$status[$val['status']]:'未知';
                }
				$list = $res['list'];
			}catch(\Exception $e){
				exit($e->getMessage());
			}

			$data['rows']  = $list;
			$data['total'] = $res['count'];
			return json($data);
		}
	}





}

